/**
  ******************************************************************************
  * @file    app_Memory.c
  * @author  tuancheng_Zero
  * @version V0.1
  * @date    09/02/2024
  * @brief   app memory manage
  ******************************************************************************
*/ 
#include <includes.h>
#include "app_Memory.h"
#include "EEPROM_Driver.h"
#include "Memory_cfg.h"

// 局部定义
#define MEMORY_DATACHECK_BUFF_SIZE          70      // 数据校验缓冲大小
#define MEMORY_CHECKSUM_INDEX               35      // 校验和索引位置

// 函数声明
static uint8_t Memory_EEpromCfgDefualt(uint64_t checkBit);
static uint8_t Memory_CalCheckSum(const uint8_t * pData, uint16_t length);

// 局部变量
static uint8_t  Memory_EEpromCheckBuf[MEMORY_DATACHECK_BUFF_SIZE] = {0};
static uint8_t  Memory_EEpromPageUsedLen[DataBase_MaxpageNum] = Memory_CfgPageLength;
static uint8_t  Memory_EEpromCheckSum[DataBase_MaxpageNum] = {0};
static uint64_t Memory_EEpromWriteBit = 0;
static uint64_t Memory_EEpromCheckBit = 0;

/// @brief RAM中的配置数据更新到EEPROM中
/// @param  none
/// @return 0失败  1成功
uint8_t Memory_UpdateCfgRamToEEprom(void){
    uint8_t errorSta = DEF_TRUE;

    if(Memory_EEpromWriteBit & DEF_BIT_00){if(EEPROM_WriteData(EEPROM_StartAddr_BaseCfg,           (uint8_t *)&share_parameterCfgData_st.baseData_st,           sizeof(share_parameterCfgData_st.baseData_st)) == DEF_TRUE)          {Memory_ClrEEpromBit(0);  Memory_EEpromCheckBuf[MEMORY_CHECKSUM_INDEX + 0]  = Memory_CalCheckSum((uint8_t *)&share_parameterCfgData_st.baseData_st,          sizeof(share_parameterCfgData_st.baseData_st));         }else errorSta = DEF_FALSE; OSTimeDlyHMSM(0, 0, 0, 5);}
    if(Memory_EEpromWriteBit & DEF_BIT_01){if(EEPROM_WriteData(EEPROM_StartAddr_NominalCfg,        (uint8_t *)&share_parameterCfgData_st.nominalCfg_st,         sizeof(share_parameterCfgData_st.nominalCfg_st)) == DEF_TRUE)        {Memory_ClrEEpromBit(1);  Memory_EEpromCheckBuf[MEMORY_CHECKSUM_INDEX + 1]  = Memory_CalCheckSum((uint8_t *)&share_parameterCfgData_st.nominalCfg_st,        sizeof(share_parameterCfgData_st.nominalCfg_st));       }else errorSta = DEF_FALSE; OSTimeDlyHMSM(0, 0, 0, 5);}
    if(Memory_EEpromWriteBit & DEF_BIT_02){if(EEPROM_WriteData(EEPROM_StartAddr_SeriousAlarmCfg,   (uint8_t *)&share_parameterCfgData_st.seriousAlarmCfg_st,    sizeof(share_parameterCfgData_st.seriousAlarmCfg_st)) == DEF_TRUE)   {Memory_ClrEEpromBit(2);  Memory_EEpromCheckBuf[MEMORY_CHECKSUM_INDEX + 2]  = Memory_CalCheckSum((uint8_t *)&share_parameterCfgData_st.seriousAlarmCfg_st,   sizeof(share_parameterCfgData_st.seriousAlarmCfg_st));  }else errorSta = DEF_FALSE; OSTimeDlyHMSM(0, 0, 0, 5);}
    if(Memory_EEpromWriteBit & DEF_BIT_03){if(EEPROM_WriteData(EEPROM_StartAddr_PidDebugCfg,       (uint8_t *)&share_parameterCfgData_st.pidDebugCfg_st,        sizeof(share_parameterCfgData_st.pidDebugCfg_st)) == DEF_TRUE)       {Memory_ClrEEpromBit(3);  Memory_EEpromCheckBuf[MEMORY_CHECKSUM_INDEX + 3]  = Memory_CalCheckSum((uint8_t *)&share_parameterCfgData_st.pidDebugCfg_st,       sizeof(share_parameterCfgData_st.pidDebugCfg_st));      }else errorSta = DEF_FALSE; OSTimeDlyHMSM(0, 0, 0, 5);}
    if(Memory_EEpromWriteBit & DEF_BIT_04){if(EEPROM_WriteData(EEPROM_StartAddr_MotorSwitchCfg,    (uint8_t *)&share_parameterCfgData_st.motorSwitchCfg_st,     sizeof(share_parameterCfgData_st.motorSwitchCfg_st)) == DEF_TRUE)    {Memory_ClrEEpromBit(4);  Memory_EEpromCheckBuf[MEMORY_CHECKSUM_INDEX + 4]  = Memory_CalCheckSum((uint8_t *)&share_parameterCfgData_st.motorSwitchCfg_st,    sizeof(share_parameterCfgData_st.motorSwitchCfg_st));   }else errorSta = DEF_FALSE; OSTimeDlyHMSM(0, 0, 0, 5);}
    if(Memory_EEpromWriteBit & DEF_BIT_05){if(EEPROM_WriteData(EEPROM_StartAddr_F_BackClosingCfg,  (uint8_t *)&share_parameterCfgData_st.f_BackClosingCfg_st,   sizeof(share_parameterCfgData_st.f_BackClosingCfg_st)) == DEF_TRUE)  {Memory_ClrEEpromBit(5);  Memory_EEpromCheckBuf[MEMORY_CHECKSUM_INDEX + 5]  = Memory_CalCheckSum((uint8_t *)&share_parameterCfgData_st.f_BackClosingCfg_st,  sizeof(share_parameterCfgData_st.f_BackClosingCfg_st)); }else errorSta = DEF_FALSE; OSTimeDlyHMSM(0, 0, 0, 5);}
    if(Memory_EEpromWriteBit & DEF_BIT_06){if(EEPROM_WriteData(EEPROM_StartAddr_EstaMagnCfg,       (uint8_t *)&share_parameterCfgData_st.estaMagnCfg_st,        sizeof(share_parameterCfgData_st.estaMagnCfg_st)) == DEF_TRUE)       {Memory_ClrEEpromBit(6);  Memory_EEpromCheckBuf[MEMORY_CHECKSUM_INDEX + 6]  = Memory_CalCheckSum((uint8_t *)&share_parameterCfgData_st.estaMagnCfg_st,       sizeof(share_parameterCfgData_st.estaMagnCfg_st));      }else errorSta = DEF_FALSE; OSTimeDlyHMSM(0, 0, 0, 5);}
    if(Memory_EEpromWriteBit & DEF_BIT_07){if(EEPROM_WriteData(EEPROM_StartAddr_VolatilityCfg,     (uint8_t *)&share_parameterCfgData_st.volatilityCfg_st,      sizeof(share_parameterCfgData_st.volatilityCfg_st)) == DEF_TRUE)     {Memory_ClrEEpromBit(7);  Memory_EEpromCheckBuf[MEMORY_CHECKSUM_INDEX + 7]  = Memory_CalCheckSum((uint8_t *)&share_parameterCfgData_st.volatilityCfg_st,     sizeof(share_parameterCfgData_st.volatilityCfg_st));    }else errorSta = DEF_FALSE; OSTimeDlyHMSM(0, 0, 0, 5);}
    if(Memory_EEpromWriteBit & DEF_BIT_08){if(EEPROM_WriteData(EEPROM_StartAddr_CeilingExciCfg,    (uint8_t *)&share_parameterCfgData_st.ceilingExciCfg_st,     sizeof(share_parameterCfgData_st.ceilingExciCfg_st)) == DEF_TRUE)    {Memory_ClrEEpromBit(8);  Memory_EEpromCheckBuf[MEMORY_CHECKSUM_INDEX + 8]  = Memory_CalCheckSum((uint8_t *)&share_parameterCfgData_st.ceilingExciCfg_st,    sizeof(share_parameterCfgData_st.ceilingExciCfg_st));   }else errorSta = DEF_FALSE; OSTimeDlyHMSM(0, 0, 0, 5);}
    if(Memory_EEpromWriteBit & DEF_BIT_09){if(EEPROM_WriteData(EEPROM_StartAddr_UnderFreqCfg,      (uint8_t *)&share_parameterCfgData_st.underFreqCfg_st,       sizeof(share_parameterCfgData_st.underFreqCfg_st)) == DEF_TRUE)      {Memory_ClrEEpromBit(9);  Memory_EEpromCheckBuf[MEMORY_CHECKSUM_INDEX + 9]  = Memory_CalCheckSum((uint8_t *)&share_parameterCfgData_st.underFreqCfg_st,      sizeof(share_parameterCfgData_st.underFreqCfg_st));     }else errorSta = DEF_FALSE; OSTimeDlyHMSM(0, 0, 0, 5);}
    if(Memory_EEpromWriteBit & DEF_BIT_10){if(EEPROM_WriteData(EEPROM_StartAddr_StartStopCfg,      (uint8_t *)&share_parameterCfgData_st.startStopCfg_st,       sizeof(share_parameterCfgData_st.startStopCfg_st)) == DEF_TRUE)      {Memory_ClrEEpromBit(10); Memory_EEpromCheckBuf[MEMORY_CHECKSUM_INDEX + 10] = Memory_CalCheckSum((uint8_t *)&share_parameterCfgData_st.startStopCfg_st,      sizeof(share_parameterCfgData_st.startStopCfg_st));     }else errorSta = DEF_FALSE; OSTimeDlyHMSM(0, 0, 0, 5);}
    if(Memory_EEpromWriteBit & DEF_BIT_11){if(EEPROM_WriteData(EEPROM_StartAddr_SyncClosingCfg,    (uint8_t *)&share_parameterCfgData_st.syncClosingCfg_st,     sizeof(share_parameterCfgData_st.syncClosingCfg_st)) == DEF_TRUE)    {Memory_ClrEEpromBit(11); Memory_EEpromCheckBuf[MEMORY_CHECKSUM_INDEX + 11] = Memory_CalCheckSum((uint8_t *)&share_parameterCfgData_st.syncClosingCfg_st,    sizeof(share_parameterCfgData_st.syncClosingCfg_st));   }else errorSta = DEF_FALSE; OSTimeDlyHMSM(0, 0, 0, 5);}
    if(Memory_EEpromWriteBit & DEF_BIT_12){if(EEPROM_WriteData(EEPROM_StartAddr_P100_UploadCfg,    (uint8_t *)&share_parameterCfgData_st.P100_uploadCfg_st,     sizeof(share_parameterCfgData_st.P100_uploadCfg_st)) == DEF_TRUE)    {Memory_ClrEEpromBit(12); Memory_EEpromCheckBuf[MEMORY_CHECKSUM_INDEX + 12] = Memory_CalCheckSum((uint8_t *)&share_parameterCfgData_st.P100_uploadCfg_st,    sizeof(share_parameterCfgData_st.P100_uploadCfg_st));   }else errorSta = DEF_FALSE; OSTimeDlyHMSM(0, 0, 0, 5);}
    if(Memory_EEpromWriteBit & DEF_BIT_13){if(EEPROM_WriteData(EEPROM_StartAddr_P60_UploadCfg,     (uint8_t *)&share_parameterCfgData_st.P60_uploadCfg_st,      sizeof(share_parameterCfgData_st.P60_uploadCfg_st)) == DEF_TRUE)     {Memory_ClrEEpromBit(13); Memory_EEpromCheckBuf[MEMORY_CHECKSUM_INDEX + 13] = Memory_CalCheckSum((uint8_t *)&share_parameterCfgData_st.P60_uploadCfg_st,     sizeof(share_parameterCfgData_st.P60_uploadCfg_st));    }else errorSta = DEF_FALSE; OSTimeDlyHMSM(0, 0, 0, 5);}
    if(Memory_EEpromWriteBit & DEF_BIT_14){if(EEPROM_WriteData(EEPROM_StartAddr_C60_UploadCfg,     (uint8_t *)&share_parameterCfgData_st.C60_uploadCfg_st,      sizeof(share_parameterCfgData_st.C60_uploadCfg_st)) == DEF_TRUE)     {Memory_ClrEEpromBit(14); Memory_EEpromCheckBuf[MEMORY_CHECKSUM_INDEX + 14] = Memory_CalCheckSum((uint8_t *)&share_parameterCfgData_st.C60_uploadCfg_st,     sizeof(share_parameterCfgData_st.C60_uploadCfg_st));    }else errorSta = DEF_FALSE; OSTimeDlyHMSM(0, 0, 0, 5);}
    if(Memory_EEpromWriteBit & DEF_BIT_15){if(EEPROM_WriteData(EEPROM_StartAddr_SuddenUnloadCfg,   (uint8_t *)&share_parameterCfgData_st.suddenUnloadCfg_st,    sizeof(share_parameterCfgData_st.suddenUnloadCfg_st)) == DEF_TRUE)   {Memory_ClrEEpromBit(15); Memory_EEpromCheckBuf[MEMORY_CHECKSUM_INDEX + 15] = Memory_CalCheckSum((uint8_t *)&share_parameterCfgData_st.suddenUnloadCfg_st,   sizeof(share_parameterCfgData_st.suddenUnloadCfg_st));  }else errorSta = DEF_FALSE; OSTimeDlyHMSM(0, 0, 0, 5);}
    if(Memory_EEpromWriteBit & DEF_BIT_16){if(EEPROM_WriteData(EEPROM_StartAddr_CalibrationFactor, (uint8_t *)&share_parameterCfgData_st.calibrationFactor_st,  sizeof(share_parameterCfgData_st.calibrationFactor_st)) == DEF_TRUE) {Memory_ClrEEpromBit(16); Memory_EEpromCheckBuf[MEMORY_CHECKSUM_INDEX + 16] = Memory_CalCheckSum((uint8_t *)&share_parameterCfgData_st.calibrationFactor_st, sizeof(share_parameterCfgData_st.calibrationFactor_st));}else errorSta = DEF_FALSE; OSTimeDlyHMSM(0, 0, 0, 5);}
    if(Memory_EEpromWriteBit & DEF_BIT_17){if(EEPROM_WriteData(EEPROM_StartAddr_LoadEquConfig,     (uint8_t *)&share_parameterCfgData_st.loadEquConfig_st,      sizeof(share_parameterCfgData_st.loadEquConfig_st)) == DEF_TRUE)     {Memory_ClrEEpromBit(17); Memory_EEpromCheckBuf[MEMORY_CHECKSUM_INDEX + 17] = Memory_CalCheckSum((uint8_t *)&share_parameterCfgData_st.loadEquConfig_st,     sizeof(share_parameterCfgData_st.loadEquConfig_st));    }else errorSta = DEF_FALSE; OSTimeDlyHMSM(0, 0, 0, 5);}
    /* 更新数据校验区 */
    if(EEPROM_WriteData(EEPROM_StartAddr_DataCheckout, Memory_EEpromCheckBuf, MEMORY_DATACHECK_BUFF_SIZE) == DEF_FALSE) errorSta = DEF_FALSE;
    return errorSta;
}

/// @brief EEPROM中的配置数据更新到RAM中
/// @param  none
/// @return 0失败  1成功
uint8_t Memory_UpdateCfgEEpromToRam(void){
    uint8_t i = 0;
    uint8_t status = 0;
    uint32_t errorBit = 0;

    // 获取数据校验区
    if(EEPROM_ReadData(EEPROM_StartAddr_DataCheckout, Memory_EEpromCheckBuf, MEMORY_DATACHECK_BUFF_SIZE) == DEF_FALSE)
        return DEF_FALSE;
    // 校验数据缓冲区标志
    if(Memory_EEpromCheckBuf[0] != 0xa5 || Memory_EEpromCheckBuf[1] != 0xa5){
        // 没有往EEPROM写入过数据使用默认初始化数据
        status = Memory_EEpromCfgDefualt(0xffffffff);
        return status;
    }
    // 检测数据长度是否正确
    Memory_EEpromCheckBit = 0;
    for(i = 0; i < DataBase_CfgPageUsedNum; i++){
        if(Memory_EEpromCheckBuf[2 + i] != Memory_EEpromPageUsedLen[i]){
            Memory_EEpromCheckBit |= (0x01 << i);
        }
    }
    // 从EEPROM中读取数据
    errorBit |= EEPROM_ReadData(EEPROM_StartAddr_BaseCfg,           (uint8_t *)&share_parameterCfgData_st.baseData_st,          sizeof(share_parameterCfgData_st.baseData_st))         << 0;
    errorBit |= EEPROM_ReadData(EEPROM_StartAddr_NominalCfg,        (uint8_t *)&share_parameterCfgData_st.nominalCfg_st,        sizeof(share_parameterCfgData_st.nominalCfg_st))       << 1;
    errorBit |= EEPROM_ReadData(EEPROM_StartAddr_SeriousAlarmCfg,   (uint8_t *)&share_parameterCfgData_st.seriousAlarmCfg_st,   sizeof(share_parameterCfgData_st.seriousAlarmCfg_st))  << 2;
    errorBit |= EEPROM_ReadData(EEPROM_StartAddr_PidDebugCfg,       (uint8_t *)&share_parameterCfgData_st.pidDebugCfg_st,       sizeof(share_parameterCfgData_st.pidDebugCfg_st))      << 3;
    errorBit |= EEPROM_ReadData(EEPROM_StartAddr_MotorSwitchCfg,    (uint8_t *)&share_parameterCfgData_st.motorSwitchCfg_st,    sizeof(share_parameterCfgData_st.motorSwitchCfg_st))   << 4;
    errorBit |= EEPROM_ReadData(EEPROM_StartAddr_F_BackClosingCfg,  (uint8_t *)&share_parameterCfgData_st.f_BackClosingCfg_st,  sizeof(share_parameterCfgData_st.f_BackClosingCfg_st)) << 5;
    errorBit |= EEPROM_ReadData(EEPROM_StartAddr_EstaMagnCfg,       (uint8_t *)&share_parameterCfgData_st.estaMagnCfg_st,       sizeof(share_parameterCfgData_st.estaMagnCfg_st))      << 6;    
    errorBit |= EEPROM_ReadData(EEPROM_StartAddr_VolatilityCfg,     (uint8_t *)&share_parameterCfgData_st.volatilityCfg_st,     sizeof(share_parameterCfgData_st.volatilityCfg_st))    << 7;
    errorBit |= EEPROM_ReadData(EEPROM_StartAddr_CeilingExciCfg,    (uint8_t *)&share_parameterCfgData_st.ceilingExciCfg_st,    sizeof(share_parameterCfgData_st.ceilingExciCfg_st))   << 8;
    errorBit |= EEPROM_ReadData(EEPROM_StartAddr_UnderFreqCfg,      (uint8_t *)&share_parameterCfgData_st.underFreqCfg_st,      sizeof(share_parameterCfgData_st.underFreqCfg_st))     << 9;
    errorBit |= EEPROM_ReadData(EEPROM_StartAddr_StartStopCfg,      (uint8_t *)&share_parameterCfgData_st.startStopCfg_st,      sizeof(share_parameterCfgData_st.startStopCfg_st))     << 10;
    errorBit |= EEPROM_ReadData(EEPROM_StartAddr_SyncClosingCfg,    (uint8_t *)&share_parameterCfgData_st.syncClosingCfg_st,    sizeof(share_parameterCfgData_st.syncClosingCfg_st))   << 11;
    errorBit |= EEPROM_ReadData(EEPROM_StartAddr_P100_UploadCfg,    (uint8_t *)&share_parameterCfgData_st.P100_uploadCfg_st,    sizeof(share_parameterCfgData_st.P100_uploadCfg_st))   << 12;
    errorBit |= EEPROM_ReadData(EEPROM_StartAddr_P60_UploadCfg,     (uint8_t *)&share_parameterCfgData_st.P60_uploadCfg_st,     sizeof(share_parameterCfgData_st.P60_uploadCfg_st))    << 13;
    errorBit |= EEPROM_ReadData(EEPROM_StartAddr_C60_UploadCfg,     (uint8_t *)&share_parameterCfgData_st.C60_uploadCfg_st,     sizeof(share_parameterCfgData_st.C60_uploadCfg_st))    << 14;
    errorBit |= EEPROM_ReadData(EEPROM_StartAddr_SuddenUnloadCfg,   (uint8_t *)&share_parameterCfgData_st.suddenUnloadCfg_st,   sizeof(share_parameterCfgData_st.suddenUnloadCfg_st))  << 15;
    errorBit |= EEPROM_ReadData(EEPROM_StartAddr_CalibrationFactor, (uint8_t *)&share_parameterCfgData_st.calibrationFactor_st, sizeof(share_parameterCfgData_st.calibrationFactor_st))<< 16;
    errorBit |= EEPROM_ReadData(EEPROM_StartAddr_LoadEquConfig,     (uint8_t *)&share_parameterCfgData_st.loadEquConfig_st,     sizeof(share_parameterCfgData_st.loadEquConfig_st))    << 17;
    // 计算校验和
    Memory_EEpromCheckSum[0]  = Memory_CalCheckSum((uint8_t *)&share_parameterCfgData_st.baseData_st,          sizeof(share_parameterCfgData_st.baseData_st));
    Memory_EEpromCheckSum[1]  = Memory_CalCheckSum((uint8_t *)&share_parameterCfgData_st.nominalCfg_st,        sizeof(share_parameterCfgData_st.nominalCfg_st));
    Memory_EEpromCheckSum[2]  = Memory_CalCheckSum((uint8_t *)&share_parameterCfgData_st.seriousAlarmCfg_st,   sizeof(share_parameterCfgData_st.seriousAlarmCfg_st));
    Memory_EEpromCheckSum[3]  = Memory_CalCheckSum((uint8_t *)&share_parameterCfgData_st.pidDebugCfg_st,       sizeof(share_parameterCfgData_st.pidDebugCfg_st));
    Memory_EEpromCheckSum[4]  = Memory_CalCheckSum((uint8_t *)&share_parameterCfgData_st.motorSwitchCfg_st,    sizeof(share_parameterCfgData_st.motorSwitchCfg_st));
    Memory_EEpromCheckSum[5]  = Memory_CalCheckSum((uint8_t *)&share_parameterCfgData_st.f_BackClosingCfg_st,  sizeof(share_parameterCfgData_st.f_BackClosingCfg_st));
    Memory_EEpromCheckSum[6]  = Memory_CalCheckSum((uint8_t *)&share_parameterCfgData_st.estaMagnCfg_st,       sizeof(share_parameterCfgData_st.estaMagnCfg_st));
    Memory_EEpromCheckSum[7]  = Memory_CalCheckSum((uint8_t *)&share_parameterCfgData_st.volatilityCfg_st,     sizeof(share_parameterCfgData_st.volatilityCfg_st));
    Memory_EEpromCheckSum[8]  = Memory_CalCheckSum((uint8_t *)&share_parameterCfgData_st.ceilingExciCfg_st,    sizeof(share_parameterCfgData_st.ceilingExciCfg_st));
    Memory_EEpromCheckSum[9]  = Memory_CalCheckSum((uint8_t *)&share_parameterCfgData_st.underFreqCfg_st,      sizeof(share_parameterCfgData_st.underFreqCfg_st));
    Memory_EEpromCheckSum[10] = Memory_CalCheckSum((uint8_t *)&share_parameterCfgData_st.startStopCfg_st,      sizeof(share_parameterCfgData_st.startStopCfg_st));
    Memory_EEpromCheckSum[11] = Memory_CalCheckSum((uint8_t *)&share_parameterCfgData_st.syncClosingCfg_st,    sizeof(share_parameterCfgData_st.syncClosingCfg_st));
    Memory_EEpromCheckSum[12] = Memory_CalCheckSum((uint8_t *)&share_parameterCfgData_st.P100_uploadCfg_st,    sizeof(share_parameterCfgData_st.P100_uploadCfg_st));
    Memory_EEpromCheckSum[13] = Memory_CalCheckSum((uint8_t *)&share_parameterCfgData_st.P60_uploadCfg_st,     sizeof(share_parameterCfgData_st.P60_uploadCfg_st));
    Memory_EEpromCheckSum[14] = Memory_CalCheckSum((uint8_t *)&share_parameterCfgData_st.C60_uploadCfg_st,     sizeof(share_parameterCfgData_st.C60_uploadCfg_st));
    Memory_EEpromCheckSum[15] = Memory_CalCheckSum((uint8_t *)&share_parameterCfgData_st.suddenUnloadCfg_st,   sizeof(share_parameterCfgData_st.suddenUnloadCfg_st));
    Memory_EEpromCheckSum[16] = Memory_CalCheckSum((uint8_t *)&share_parameterCfgData_st.calibrationFactor_st, sizeof(share_parameterCfgData_st.calibrationFactor_st));
    Memory_EEpromCheckSum[17] = Memory_CalCheckSum((uint8_t *)&share_parameterCfgData_st.loadEquConfig_st,     sizeof(share_parameterCfgData_st.loadEquConfig_st));
    // 如果有长度不匹配的位则该页恢复默认值
    if(Memory_EEpromCheckBit != 0){
        Memory_EEpromCfgDefualt(Memory_EEpromCheckBit);
    }
    // 如果有校验和不匹配的位则该页恢复默认值
    for(i = 0; i < DataBase_CfgPageUsedNum; i++){
        if(Memory_EEpromCheckSum[i] != Memory_EEpromCheckBuf[i + MEMORY_CHECKSUM_INDEX])
            Memory_EEpromCheckBit |= (0x01 << i);
    }
    if(Memory_EEpromCheckBit != 0){
        Memory_EEpromCfgDefualt(Memory_EEpromCheckBit);
    }
    // 判断所有读取是否成功
    if(errorBit != 0x3ffff)
        return DEF_FALSE;
    return DEF_TRUE;
}

/// @brief 将EEPROM中的配置参数设置成默认值
/// @param  none
/// @return 0失败  1成功
static uint8_t Memory_EEpromCfgDefualt(uint64_t checkBit){
    uint8_t os_err = 0;
    uint8_t i = 0;

    // 基础参数
    if(checkBit & DEF_BIT_00){
        share_parameterCfgData_st.baseData_st.InputLine_Mode  = 2;
        share_parameterCfgData_st.baseData_st.ExciPower_mode  = 1;
        share_parameterCfgData_st.baseData_st.WaveRecord_rate = 1;
        share_parameterCfgData_st.baseData_st.TransientResponse_rate = 0;
        share_parameterCfgData_st.baseData_st.DeviceAddr      = 1;
        share_parameterCfgData_st.baseData_st.Language_Mode   = 0;
        share_parameterCfgData_st.baseData_st.TimeArea        = 8;
        share_parameterCfgData_st.baseData_st.SerialNumber    = 0;
        strcpy((char *)share_parameterCfgData_st.baseData_st.WifiName, "Tuancheng");
        strcpy((char *)share_parameterCfgData_st.baseData_st.WifiPassWord, "12345678");
        strcpy((char *)share_parameterCfgData_st.baseData_st.ServerHostName, "tcp.i6yun.com");
        share_parameterCfgData_st.baseData_st.ServerPort = 15000;
        share_parameterCfgData_st.baseData_st.PriFreCalibCoefficient = 1;
        Memory_SetEEpromBit(0);
    }
    // 额定参数
    if(checkBit & DEF_BIT_01){
        share_parameterCfgData_st.nominalCfg_st.RatedPhaseVolt       = 230.0;
        share_parameterCfgData_st.nominalCfg_st.RatedLineVolt        = 400.0;
        share_parameterCfgData_st.nominalCfg_st.RatedFrequency       = 50.0;
        share_parameterCfgData_st.nominalCfg_st.RatedCurrent         = 500.0;
        share_parameterCfgData_st.nominalCfg_st.TransfPrimaryVolt    = 400.0;
        share_parameterCfgData_st.nominalCfg_st.TransfSeconeVolt     = 400.0;
        share_parameterCfgData_st.nominalCfg_st.TransfPrimaryCurrent = 100.0;
        share_parameterCfgData_st.nominalCfg_st.TransfSeconeCurrent  = 5.0;
        share_parameterCfgData_st.nominalCfg_st.VoltToleBand         = 1.0;
        share_parameterCfgData_st.nominalCfg_st.FreqToleBand         = 1.0;
        share_parameterCfgData_st.nominalCfg_st.RatedExciVolt        = 30.0;
        share_parameterCfgData_st.nominalCfg_st.RatedExciCurrent     = 2.5;
        share_parameterCfgData_st.nominalCfg_st.MaxExciVoltage       = 70.0;
        share_parameterCfgData_st.nominalCfg_st.MaxExciCurrent       = 3.5;
        share_parameterCfgData_st.nominalCfg_st.RatedPowerFactor     = 0.8;
        Memory_SetEEpromBit(1);
    }
    // 严重告警参数
    if(checkBit & DEF_BIT_02){
        share_parameterCfgData_st.seriousAlarmCfg_st.OverVoltPhas_threshold  = 120.0;
        share_parameterCfgData_st.seriousAlarmCfg_st.OverVoltLine_threshold  = 120.0;
        share_parameterCfgData_st.seriousAlarmCfg_st.OverVoltConfirm         = 0.5;
        share_parameterCfgData_st.seriousAlarmCfg_st.OverCurrent_precent     = 120;
        share_parameterCfgData_st.seriousAlarmCfg_st.OverCurrentConfirm      = 5.0;
        share_parameterCfgData_st.seriousAlarmCfg_st.Overload_precent        = 120.0;
        share_parameterCfgData_st.seriousAlarmCfg_st.OverAactPowerConfirm    = 5.0;
        share_parameterCfgData_st.seriousAlarmCfg_st.OverReacPower_precent   = 120.0;
        share_parameterCfgData_st.seriousAlarmCfg_st.OverReacPowerConfirm    = 5.0;
        share_parameterCfgData_st.seriousAlarmCfg_st.LowPowerFact_threshold  = 0.3;
        share_parameterCfgData_st.seriousAlarmCfg_st.PowerFactorConfirm      = 3.0;
        share_parameterCfgData_st.seriousAlarmCfg_st.OverExciVolt_precent    = 120.0;
        share_parameterCfgData_st.seriousAlarmCfg_st.OverExciVoltConfirm     = 4.0;
        share_parameterCfgData_st.seriousAlarmCfg_st.OverExciCurrent_precent = 120.0;
        share_parameterCfgData_st.seriousAlarmCfg_st.OverExciCurrentConfirm  = 3.0;
        Memory_SetEEpromBit(2);
    }
    // pid调节参数
    if(checkBit & DEF_BIT_03){
        share_parameterCfgData_st.pidDebugCfg_st.ExciDivider_P   = 100;
        share_parameterCfgData_st.pidDebugCfg_st.ExciDivider_I   = 100;
        share_parameterCfgData_st.pidDebugCfg_st.ExciDivider_D   = 100;
        share_parameterCfgData_st.pidDebugCfg_st.ExciShunt_P     = 100;
        share_parameterCfgData_st.pidDebugCfg_st.ExciShunt_I     = 100;
        share_parameterCfgData_st.pidDebugCfg_st.ExciShunt_D     = 100;
        share_parameterCfgData_st.pidDebugCfg_st.BuildVolt_P     = 100;
        share_parameterCfgData_st.pidDebugCfg_st.BuildVolt_I     = 100;
        share_parameterCfgData_st.pidDebugCfg_st.BuildVolt_D     = 100;
        share_parameterCfgData_st.pidDebugCfg_st.ActivPower_P    = 100;
        share_parameterCfgData_st.pidDebugCfg_st.ActivPower_I    = 100;
        share_parameterCfgData_st.pidDebugCfg_st.ActivPower_D    = 100;
        share_parameterCfgData_st.pidDebugCfg_st.ReacPower_P     = 100;
        share_parameterCfgData_st.pidDebugCfg_st.ReacPower_I     = 100;
        share_parameterCfgData_st.pidDebugCfg_st.ReacPower_D     = 100;
        share_parameterCfgData_st.pidDebugCfg_st.VoltAdjust_P    = 100;
        share_parameterCfgData_st.pidDebugCfg_st.VoltAdjust_I    = 100;
        share_parameterCfgData_st.pidDebugCfg_st.VoltAdjust_D    = 100;
        share_parameterCfgData_st.pidDebugCfg_st.PhaseLockLoop_P = 100;
        share_parameterCfgData_st.pidDebugCfg_st.PhaseLockLoop_I = 100;
        share_parameterCfgData_st.pidDebugCfg_st.PhaseLockLoop_D = 100;
        Memory_SetEEpromBit(3);
    }
    // 电机和开关控制参数
    if(checkBit & DEF_BIT_04){
        share_parameterCfgData_st.motorSwitchCfg_st.EngineStart_timeout       = 110;
        share_parameterCfgData_st.motorSwitchCfg_st.SteadyFreq_threshold      = 10.0;
        share_parameterCfgData_st.motorSwitchCfg_st.EngineStop_timeout        = 20;
        share_parameterCfgData_st.motorSwitchCfg_st.Reserve1                  = 0;
        share_parameterCfgData_st.motorSwitchCfg_st.Reserve2                  = 0;
        share_parameterCfgData_st.motorSwitchCfg_st.SyncOpen_timeout          = 3;
        share_parameterCfgData_st.motorSwitchCfg_st.SyncClose_timeout         = 3;
        share_parameterCfgData_st.motorSwitchCfg_st.ExciDividSW1_timeout      = 3;
        share_parameterCfgData_st.motorSwitchCfg_st.ExciDividSW2_timeout      = 3;
        share_parameterCfgData_st.motorSwitchCfg_st.Reserve3                  = 0;
        share_parameterCfgData_st.motorSwitchCfg_st.ExciVoltDividZero_timeout = 20;
        share_parameterCfgData_st.motorSwitchCfg_st.ExciVoltageAdjust_timeout = 30;
        share_parameterCfgData_st.motorSwitchCfg_st.Reserve4                  = 0;
        share_parameterCfgData_st.motorSwitchCfg_st.ExciCurrDividZero_timeout = 10;
        share_parameterCfgData_st.motorSwitchCfg_st.ExciCurrentAdjust_timeout = 30;
        share_parameterCfgData_st.motorSwitchCfg_st.Reserve5                  = 0;
        share_parameterCfgData_st.motorSwitchCfg_st.ActivPowerAdjust_timeout  = 20;
        share_parameterCfgData_st.motorSwitchCfg_st.RealPowerAdjust_timeout   = 20;
        Memory_SetEEpromBit(4);
    }
    // 合闸后检查
    if(checkBit & DEF_BIT_05){
        share_parameterCfgData_st.f_BackClosingCfg_st.AutoSyncClose_timeout = 50;
        Memory_SetEEpromBit(5);
    }
    // 建压测试
    if(checkBit & DEF_BIT_06){
        share_parameterCfgData_st.estaMagnCfg_st.EngineInitSpeed       = 25.0;
        share_parameterCfgData_st.estaMagnCfg_st.EngineInitStart_time  = 3;
        share_parameterCfgData_st.estaMagnCfg_st.BuildVolt_initial     = 3.0;
        share_parameterCfgData_st.estaMagnCfg_st.BVMotorRotaVoltage    = 0.5;
        share_parameterCfgData_st.estaMagnCfg_st.BVMotorStopTime       = 800;
        share_parameterCfgData_st.estaMagnCfg_st.BVSucsuss_threshold   = 1.5;
        share_parameterCfgData_st.estaMagnCfg_st.BVTest_timeout        = 50;
        share_parameterCfgData_st.estaMagnCfg_st.BuildVolt_PreStandard = 5.0;
        Memory_SetEEpromBit(6);
    }
    // 波动率测试
    if(checkBit & DEF_BIT_07){
        share_parameterCfgData_st.volatilityCfg_st.EngineInitSpeed      = 50.0;
        share_parameterCfgData_st.volatilityCfg_st.EngineInitStart_time = 3;
        share_parameterCfgData_st.volatilityCfg_st.WaitVoltStab_time    = 6;
        share_parameterCfgData_st.volatilityCfg_st.VolatTest_time       = 30;
        Memory_SetEEpromBit(7);
    }
    // 极限励磁测试
    if(checkBit & DEF_BIT_08){
        share_parameterCfgData_st.ceilingExciCfg_st.EngineInitSpeed      = 50.0;
        share_parameterCfgData_st.ceilingExciCfg_st.EngineInitStart_time = 3;
        share_parameterCfgData_st.ceilingExciCfg_st.WaitVoltStab_time    = 8;
        share_parameterCfgData_st.ceilingExciCfg_st.MaxExciTest_timeout  = 20;
        share_parameterCfgData_st.ceilingExciCfg_st.ExciVolt_value       = 100.0;
        share_parameterCfgData_st.ceilingExciCfg_st.ExciCurrent_value    = 100.0;
        Memory_SetEEpromBit(8);
    }
    // 低频保护测试参数
    if(checkBit & DEF_BIT_09){
        share_parameterCfgData_st.underFreqCfg_st.EngineInitSpeed        = 50.0;
        share_parameterCfgData_st.underFreqCfg_st.EngineInitStart_time   = 3;
        share_parameterCfgData_st.underFreqCfg_st.WaitVoltStab_time      = 10;
        share_parameterCfgData_st.underFreqCfg_st.EngineShuntDownSpeed   = 0.0;
        share_parameterCfgData_st.underFreqCfg_st.EngineStop_time        = 20;
        share_parameterCfgData_st.underFreqCfg_st.UnderFreqTest_time     = 18;
        Memory_SetEEpromBit(9);
    }
    // 启停测试参数
    if(checkBit & DEF_BIT_10){
        share_parameterCfgData_st.startStopCfg_st.EngineInitSpeed       = 0.0;
        share_parameterCfgData_st.startStopCfg_st.EngineInitStop_time   = 3;
        share_parameterCfgData_st.startStopCfg_st.EngineStartSpeed      = 50.0;
        share_parameterCfgData_st.startStopCfg_st.EngineStart_time      = 15;
        share_parameterCfgData_st.startStopCfg_st.SteadyTest_time       = 5;
        share_parameterCfgData_st.startStopCfg_st.EngineStopSpeed       = 0.0;
        share_parameterCfgData_st.startStopCfg_st.EngineStop_time       = 12;
        Memory_SetEEpromBit(10);
    }
    // 同步合闸参数
    if(checkBit & DEF_BIT_11){
        share_parameterCfgData_st.syncClosingCfg_st.SyncVoltDiff_transient  = 2.5;
        share_parameterCfgData_st.syncClosingCfg_st.SyncVoltDiff_average    = 1.5;
        share_parameterCfgData_st.syncClosingCfg_st.SyncPhaseDiff_transient = 2.0;
        share_parameterCfgData_st.syncClosingCfg_st.SyncPhaseDiff_average   = 1.0;
        share_parameterCfgData_st.syncClosingCfg_st.SyncFreqDiff_transient  = 0.2;
        Memory_SetEEpromBit(11);
    }
    // 100%功率整定测试参数
    if(checkBit & DEF_BIT_12){
        share_parameterCfgData_st.P100_uploadCfg_st.EngineInitSpeed      = 50.0;
        share_parameterCfgData_st.P100_uploadCfg_st.EngineInitStart_time = 3;
        share_parameterCfgData_st.P100_uploadCfg_st.ActivPower_percent   = 100.0;
        share_parameterCfgData_st.P100_uploadCfg_st.Current_percent      = 60.0;
        share_parameterCfgData_st.P100_uploadCfg_st.PowerFactor_value    = 0.8;
        share_parameterCfgData_st.P100_uploadCfg_st.ExciVolt_value       = 100.0;
        share_parameterCfgData_st.P100_uploadCfg_st.ExciCurrent_value    = 100.0;
        share_parameterCfgData_st.P100_uploadCfg_st.WaitVoltStab_time    = 8;
        share_parameterCfgData_st.P100_uploadCfg_st.SetTest_time         = 30;
        Memory_SetEEpromBit(12);
    }
    // 60%功率整定测试参数
    if(checkBit & DEF_BIT_13){
        share_parameterCfgData_st.P60_uploadCfg_st.EngineInitSpeed      = 50.0;
        share_parameterCfgData_st.P60_uploadCfg_st.EngineInitStart_time = 3;
        share_parameterCfgData_st.P60_uploadCfg_st.ActivPower_percent   = 100.0;
        share_parameterCfgData_st.P60_uploadCfg_st.Current_percent      = 60.0;
        share_parameterCfgData_st.P60_uploadCfg_st.PowerFactor_value    = 0.8;
        share_parameterCfgData_st.P60_uploadCfg_st.ExciVolt_value       = 100.0;
        share_parameterCfgData_st.P60_uploadCfg_st.ExciCurrent_value    = 100.0;
        share_parameterCfgData_st.P60_uploadCfg_st.WaitVoltStab_time    = 8;
        share_parameterCfgData_st.P60_uploadCfg_st.SetTest_time         = 30;
        Memory_SetEEpromBit(13);
    }
    // 60%电流整定测试参数
    if(checkBit & DEF_BIT_14){
        share_parameterCfgData_st.C60_uploadCfg_st.EngineInitSpeed      = 50.0;
        share_parameterCfgData_st.C60_uploadCfg_st.EngineInitStart_time = 3;
        share_parameterCfgData_st.C60_uploadCfg_st.ActivPower_percent   = 100.0;
        share_parameterCfgData_st.C60_uploadCfg_st.Current_percent      = 60.0;
        share_parameterCfgData_st.C60_uploadCfg_st.PowerFactor_value    = 0.8;
        share_parameterCfgData_st.C60_uploadCfg_st.ExciVolt_value       = 100.0;
        share_parameterCfgData_st.C60_uploadCfg_st.ExciCurrent_value    = 100.0;
        share_parameterCfgData_st.C60_uploadCfg_st.WaitVoltStab_time    = 8;
        share_parameterCfgData_st.C60_uploadCfg_st.SetTest_time         = 30;
        Memory_SetEEpromBit(14);
    }
    // 突卸测试
    if(checkBit & DEF_BIT_15){
        share_parameterCfgData_st.suddenUnloadCfg_st.WaitVoltStab_time = 5;
        share_parameterCfgData_st.suddenUnloadCfg_st.UnloadTest_time   = 12;
        Memory_SetEEpromBit(15);
    }
    // 校准系数
    if(checkBit & DEF_BIT_16){
        share_parameterCfgData_st.calibrationFactor_st.GenPhaseVoltU   = 10000;
        share_parameterCfgData_st.calibrationFactor_st.GenPhaseVoltV   = 10000;
        share_parameterCfgData_st.calibrationFactor_st.GenPhaseVoltW   = 10000;
        share_parameterCfgData_st.calibrationFactor_st.GenLineVoltUV   = 10000;
        share_parameterCfgData_st.calibrationFactor_st.GenLineVoltVW   = 10000;
        share_parameterCfgData_st.calibrationFactor_st.GenLineVoltWU   = 10000;
        share_parameterCfgData_st.calibrationFactor_st.LoadPhaseVoltU  = 10000;
        share_parameterCfgData_st.calibrationFactor_st.LoadPhaseVoltV  = 10000;
        share_parameterCfgData_st.calibrationFactor_st.LoadPhaseVoltW  = 10000;
        share_parameterCfgData_st.calibrationFactor_st.LoadLineVoltUV  = 10000;
        share_parameterCfgData_st.calibrationFactor_st.LoadLineVoltVW  = 10000;
        share_parameterCfgData_st.calibrationFactor_st.LoadLineVoltWU  = 10000;
        share_parameterCfgData_st.calibrationFactor_st.GenExciteVolt   = 10000;
        share_parameterCfgData_st.calibrationFactor_st.GenExciteCurr   = 10000;
        share_parameterCfgData_st.calibrationFactor_st.LoadExciteVolt  = 10000;
        share_parameterCfgData_st.calibrationFactor_st.LoadExciteCurr  = 10000;
        share_parameterCfgData_st.calibrationFactor_st.GenCurrentU     = 10000;
        share_parameterCfgData_st.calibrationFactor_st.GenCurrentV     = 10000;
        share_parameterCfgData_st.calibrationFactor_st.GenCurrentW     = 10000;
        share_parameterCfgData_st.calibrationFactor_st.BuildVoltBEF    = 10000;
        share_parameterCfgData_st.calibrationFactor_st.BuildVoltAFT    = 10000;
        Memory_SetEEpromBit(16);
    }
    // 负载机配置
    if(checkBit & DEF_BIT_17){
        share_parameterCfgData_st.loadEquConfig_st.LoadStartFreg       = 15;
        share_parameterCfgData_st.loadEquConfig_st.LoadStopFreg        = 18;
        share_parameterCfgData_st.loadEquConfig_st.LoadInitialPWM      = 5;
        share_parameterCfgData_st.loadEquConfig_st.LoadRatedFreg       = 50;
        share_parameterCfgData_st.loadEquConfig_st.LoadLowFregTrip     = 47;
        share_parameterCfgData_st.loadEquConfig_st.LoadLowFregRate     = 3;
        share_parameterCfgData_st.loadEquConfig_st.LoadHwshutOffDelay  = 3;
        share_parameterCfgData_st.loadEquConfig_st.LoadSlowStartEnable = 1;
        share_parameterCfgData_st.loadEquConfig_st.LoadSlowStartDelay  = 5;
        share_parameterCfgData_st.loadEquConfig_st.GovMiddleValue      = 50;
        Memory_SetEEpromBit(17);
    }
    // 写EEPROM校验区域
    Memory_EEpromCheckBuf[0] = 0xa5;
    Memory_EEpromCheckBuf[1] = 0xa5;
    for(i = 0; i < DataBase_CfgPageUsedNum; i++){
        Memory_EEpromCheckBuf[2 + i] = Memory_EEpromPageUsedLen[i];
    }
    // 发送EEPROM写入事件
    OSFlagPost(App_Flags_COMMON,OSCommFlags_UpdateCfg,OS_FLAG_SET,&os_err);
    return DEF_TRUE;
}

/// @brief 计算单字节校验和
/// @param pData 需要校验数据
/// @param length 数据长度
/// @return 校验和
static uint8_t Memory_CalCheckSum(const uint8_t * pData, uint16_t length){
    uint16_t i = 0;
    uint8_t checkSum = 0;
    
    for(i = 0; i < length; i++){
        checkSum += pData[i];
    }
    return checkSum;
}

/// @brief 设置指定EEPROM写标志
/// @param index 索引地址 0开始
void Memory_SetEEpromBit(uint8_t index){
    uint8_t os_err = 0;
    uint32_t bitIndex = 0x01;
    // 获取操作权限
    OSSemPend((OS_EVENT *)App_Sem_MOMERYEE,0,&os_err); 
    // 操作数据
    Memory_EEpromWriteBit |= (bitIndex << index);
    // 释放操作权限
    OSSemPost((OS_EVENT *)App_Sem_MOMERYEE);
}

/// @brief 清除指定EEPROM写标志
/// @param index 索引地址 0开始
void Memory_ClrEEpromBit(uint8_t index){
    uint8_t os_err = 0;
    uint32_t bitIndex = 0x01;
    
    // 获取操作权限
    OSSemPend((OS_EVENT *)App_Sem_MOMERYEE,0,&os_err); 
    // 操作数据
    Memory_EEpromWriteBit &= ~(bitIndex << index);
    // 释放操作权限
    OSSemPost((OS_EVENT *)App_Sem_MOMERYEE);
}
